<?php
namespace app\admin\controller;
use app\admin\model\Level as LevelModel;
use app\admin\model\PlanPeople as PlanPeopleModel;
use app\admin\model\Unit as UnitModel;
use app\admin\model\kaodian as kaodianModel;
use app\admin\traits\Result;


class Sysconfig extends Common
{
    
    public function level(){
        
        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];
            
            $list = LevelModel::withSearch(['title'], ['title' => $data['key']])->order('id desc')
                ->paginate($data['limit'], false, ['query' => $data]);
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
            }
            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        return $this->fetch();
        
    }
    
    //验证是否存在
    public function checklevel()
    {
        $username = $this->request->get('title', '', 'trim');
        $res = LevelModel::where('title', '=', $username)->field('title')->find();
        if ($res) {
            $msg = ['code' => 0, 'msg' => '名称已存在'];
        } else {
            $msg = ['status' =>1, 'info' => '验证通过'];
        }
        return $msg;
    }
    
    public function addlevel(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['id']) {
                //编辑
				$data['update_time'] = time();
                $res = LevelModel::update($data, ['id' => $data['id']]);
                if ($res) {
                    $msg = Result::success('编辑成功', url('/admin/sysconfig-level'));
                } else {
                    $msg = Result::error('编辑失败');
                }
            } else {
                //添加
                $LevelModel = new LevelModel;
				$data['create_time'] = time();
                $res = $LevelModel->allowField(true)->save($data);
                if ($res) {
                    $msg = Result::success('添加成功', url('/admin/sysconfig-level'));
                } else {
                    $msg = Result::error('添加失败');
                }
            }
            return $msg;
        } else { 
            $id = $this->request->get('id', 0, 'intval');
            if ($id) {
                $list = LevelModel::where('id', '=', $id)->find();
               
                $this->assign('list', $list);
            }
            
            return $this->fetch();
        } 
        
    }
    public function dellevel(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
                $res = LevelModel::where('id='.$id)->delete();
                if ($res) {
                    $msg = Result::success('删除成功');
                } else {
                    $msg = Result::error('删除失败');
                }
        
                return $msg;
            
        } else {
            $this->error('参数错误');
        }
        
    }
    
    public function unit(){
        
        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];
            
            $list = UnitModel::withSearch(['title'], ['title' => $data['key']])->order('id desc')
                ->paginate($data['limit'], false, ['query' => $data]);
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
            }
            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        return $this->fetch();
        
    }
    
    //验证是否存在
    public function checkunit()
    {
        $username = $this->request->get('title', '', 'trim');
        $res = UnitModel::where('title', '=', $username)->field('title')->find();
        if ($res) {
            $msg = ['code' => 0, 'msg' => '名称已存在'];
        } else {
            $msg = ['status' =>1, 'info' => '验证通过'];
        }
        return $msg;
    }
    
    public function addunit(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['id']) {
                //编辑
				$data['update_time'] = time();
                $res = UnitModel::update($data, ['id' => $data['id']]);
                if ($res) {
                    $msg = Result::success('编辑成功', url('/admin/sysconfig-level'));
                } else {
                    $msg = Result::error('编辑失败');
                }
            } else {
                //添加
                $UnitModel = new UnitModel;
				$data['create_time'] = time();
                $res = $UnitModel->allowField(true)->save($data);
                if ($res) {
                    $msg = Result::success('添加成功', url('/admin/sysconfig-level'));
                } else {
                    $msg = Result::error('添加失败');
                }
            }
            return $msg;
        } else { 
            $id = $this->request->get('id', 0, 'intval');
            if ($id) {
                $list = UnitModel::where('id', '=', $id)->find();
               
                $this->assign('list', $list);
            }
            
            return $this->fetch();
        } 
        
    }
    public function delunit(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
                $res = UnitModel::where('id='.$id)->delete();
                if ($res) {
                    $msg = Result::success('删除成功');
                } else {
                    $msg = Result::error('删除失败');
                }
        
                return $msg;
            
        } else {
            $this->error('参数错误');
        }
        
    }
    
    
    public function planpeople(){
        
        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];
            
            $list = PlanPeopleModel::withSearch(['title'], ['title' => $data['key']])->order('id desc')
                ->paginate($data['limit'], false, ['query' => $data]);
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
            }
            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        return $this->fetch();
        
    }
    
    //验证是否存在
    public function checkplanpeople()
    {
        $username = $this->request->get('title', '', 'trim');
        $res = PlanPeopleModel::where('title', '=', $username)->field('title')->find();
        if ($res) {
            $msg = ['code' => 0, 'msg' => '名称已存在'];
        } else {
            $msg = ['status' =>1, 'info' => '验证通过'];
        }
        return $msg;
    }
    
    public function addplanpeople(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['id']) {
                //编辑
				$data['update_time'] = time();
                $res = PlanPeopleModel::update($data, ['id' => $data['id']]);
                if ($res) {
                    $msg = Result::success('编辑成功', url('/admin/sysconfig-level'));
                } else {
                    $msg = Result::error('编辑失败');
                }
            } else {
                //添加
                $PlanPeopleModel = new PlanPeopleModel;
				$data['create_time'] = time();
                $res = $PlanPeopleModel->allowField(true)->save($data);
                if ($res) {
                    $msg = Result::success('添加成功', url('/admin/sysconfig-level'));
                } else {
                    $msg = Result::error('添加失败');
                }
            }
            return $msg;
        } else { 
            $id = $this->request->get('id', 0, 'intval');
            if ($id) {
                $list = PlanPeopleModel::where('id', '=', $id)->find();
               
                $this->assign('list', $list);
            }
            
            return $this->fetch();
        } 
        
    }
    public function delplanpeople(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
                $res = PlanPeopleModel::where('id='.$id)->delete();
                if ($res) {
                    $msg = Result::success('删除成功');
                } else {
                    $msg = Result::error('删除失败');
                }
        
                return $msg;
            
        } else {
            $this->error('参数错误');
        }
        
    }
    
    public function kaodian(){
        
        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];
            
            $list = kaodianModel::withSearch(['title'], ['title' => $data['key']])->order('id desc')
                ->paginate($data['limit'], false, ['query' => $data]);
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
            }
            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        return $this->fetch();
        
    }
    
    //验证是否存在
    public function checkkaodian()
    {
        $username = $this->request->get('title', '', 'trim');
        $res = kaodianModel::where('title', '=', $username)->field('title')->find();
        if ($res) {
            $msg = ['code' => 0, 'msg' => '名称已存在'];
        } else {
            $msg = ['status' =>1, 'info' => '验证通过'];
        }
        return $msg;
    }
    
    public function addkaodian(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['id']) {
                //编辑
				$data['update_time'] = time();
                $res = kaodianModel::update($data, ['id' => $data['id']]);
                if ($res) {
                    $msg = Result::success('编辑成功', url('/admin/sysconfig-level'));
                } else {
                    $msg = Result::error('编辑失败');
                }
            } else {
                //添加
                $kaodianModel= new kaodianModel;
				$data['create_time'] = time();
                $res = $kaodianModel->allowField(true)->save($data);
                if ($res) {
                    $msg = Result::success('添加成功', url('/admin/sysconfig-level'));
                } else {
                    $msg = Result::error('添加失败');
                }
            }
            return $msg;
        } else { 
            $id = $this->request->get('id', 0, 'intval');
            if ($id) {
                $list = kaodianModel::where('id', '=', $id)->find();
               
                $this->assign('list', $list);
            }
            
            return $this->fetch();
        } 
        
    }
    public function delkaodian(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
                $res = kaodianModel::where('id='.$id)->delete();
                if ($res) {
                    $msg = Result::success('删除成功');
                } else {
                    $msg = Result::error('删除失败');
                }
        
                return $msg;
            
        } else {
            $this->error('参数错误');
        }
        
    }
}



?>